/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.sendmsg.controller  
 */
package com.dwusoft.sendmsg.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.database.model.Page;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.dwusoft.sendmsg.common.entity.Sendmsg;
import com.dwusoft.sendmsg.manager.SendmsgManager;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import com.apache.uct.common.entity.User;

/**
 * description:  消息管理action
 * @author Hou Dayu 创建时间：2016-5-18  
 * @Copyright(c)2014:北京东吴软件技术有限公司
 */
@Controller
@RequestMapping("send/sendmsg/")
public class SendMsgAction {

	@Autowired
	private SendmsgManager sendmsgManager;

	@RequestMapping(value = { "/list" }, method = { RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("sendmsg/sendmsg-list");
	}

	/**
	 *查询列表分页数据，返回JSON数据
	 */
	@RequestMapping(value = { "/list" }, method = { RequestMethod.POST })
	@ResponseBody
	protected Object listData(HttpServletRequest request, @ModelAttribute Sendmsg item, String page, String rows) {
		ParamsVo<Sendmsg> vo = new ParamsVo<Sendmsg>();
		vo.setObj(item);
		Map<String, Object> mapData = new HashMap<String, Object>();
		if (Validator.isNotNull(rows)) {//rows为空表示不进行分页处理
			vo.setParams("pageSize", Validator.getDefaultStr(rows, "10"));
			vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
			Page pageList = sendmsgManager.getPageInfo(vo);//分页
			mapData.put("total", pageList.getCount());//dataGrid所需参数
			mapData.put("rows", pageList.getPageObjects());//dataGrid所需数据对象集合
		} else {
			List<Sendmsg> list = sendmsgManager.getList(vo);
			if (!Validator.isEmpty(list)) {
				mapData.put("total", list.size());//dataGrid所需参数
				mapData.put("rows", list);//dataGrid所需数据对象集合
			}
		}
		return mapData;
	}

	@RequestMapping(value = { "/view/{id}" }, method = { RequestMethod.GET })
	public ModelAndView view(HttpServletRequest request, @PathVariable String id) {
		Sendmsg item = null;
		ParamsVo<Sendmsg> vo = new ParamsVo<Sendmsg>();
		vo.setInfoId(id);
		item = (Sendmsg) sendmsgManager.getInfoById(vo);
		ModelAndView view = new ModelAndView("sendmsg/sendmsg-save");
		view.addObject("item", item);
		return view;
	}

	/**
	 *查询单个对象，返回JSON数据
	 */
	@RequestMapping(value = { "/msg/{id}" }, method = { RequestMethod.GET })
	@ResponseBody
	protected Object infoData(@PathVariable String id, HttpServletRequest request) {
		Map<String, Object> mapData = new HashMap<String, Object>();
		Sendmsg item = null;
		ParamsVo<Sendmsg> vo = new ParamsVo<Sendmsg>();
		vo.setInfoId(id);
		item = (Sendmsg) sendmsgManager.getInfoById(vo);
		mapData.put("flag", "T");
		mapData.put("msg", "查询成功！");
		mapData.put("item", item);
		return mapData;
	}

	/**
	 * 修改Session  
	 */
	@RequestMapping(value = { "/msg/{id}" }, method = { RequestMethod.PUT })
	@ResponseBody
	public ResultMsg editInfo(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Sendmsg item,
			@PathVariable String id) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");//用于封装结果信息
		if (Validator.isNull(id)) {
			rmsg = new ResultMsg("F", "sessionId不能为空！");
			return rmsg;
		}
		ParamsVo<Sendmsg> vo = this.getParamsVo(request);//初始化参数vo对象，如果是登陆用户则获取用户信息及日志
		vo.setObj(item);//修改
		boolean mark = sendmsgManager.editInfo(vo);
		if (!mark) {
			rmsg = new ResultMsg("F", "操作失败！");
		}
		return rmsg;
	}

	/**
	 * 根据id删除session表信息
	 */
	@RequestMapping(value = { "/msg/{id}" }, method = { RequestMethod.DELETE })
	@ResponseBody
	public ResultMsg deleteInfo(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
		//初始化结果提示信息
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		response.setContentType("text/html");
		if (Validator.isNull(id)) {
			rmsg = new ResultMsg("F", "缺少必要的参数!");
			return rmsg;
		}
		ParamsVo<Sendmsg> vo = new ParamsVo<Sendmsg>();
		vo.setInfoId(id);
		vo.setParams("isDelete", "true");
		boolean mark = sendmsgManager.deleteInfo(vo);
		if (!mark) {
			rmsg = new ResultMsg("F", "操作失败！");
		}
		return rmsg;
	}

	private User getSessionUser(HttpServletRequest request) {
		LoginUser loginUser = PBOSSOTools.getLoginUser(request);
		if (!Validator.isEmpty(loginUser)) {
			return loginUser.getUser();
		}
		return null;
	}

	/**
	 * description:  日志生成调用方法
	 * @param request
	 */
	private ParamsVo<Sendmsg> getParamsVo(HttpServletRequest request) {
		ParamsVo<Sendmsg> params = new ParamsVo<Sendmsg>();
		params.setParams("logIp", getUserIp(request));
		params.setLogClassName(Sendmsg.class.getName());
		User user = getSessionUser(request);
		if (null == user)
			return params;
		params.setUserEname(user.getUserEname());
		params.setUserId(user.getUserId());
		return params;
	}

	/**
	 * description:  获取访问IP
	 * @param request 当前请求
	 * @return   
	 * @author Hou Dayu  
	 */
	private String getUserIp(HttpServletRequest request) {
		String ip = "";
		try {
			ip = request.getHeader("x-forwarded-for");
			if (Validator.isNull(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (Validator.isNull(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (Validator.isNull(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			if (Validator.isNotNull(ip) && ip.length() > 32) {
				ip = ip.substring(0, 32);
			}
		} catch (Exception e) {
		}
		return ip;
	}

}
